import { request } from '@/service/request';

/** 系统配置API */

/**
 * 查询系统配置列表
 */
export function fetchSystemConfigList(params?: Api.DataPlatform.SystemConfigSearchParams) {
  return request<Api.DataPlatform.SystemConfigList>({
    url: '/dataplatform/system-config/list',
    method: 'get',
    params
  });
}

/**
 * 根据配置类型查询配置列表
 */
export function fetchSystemConfigByType(configType: string) {
  return request<Api.DataPlatform.SystemConfig[]>({
    url: `/dataplatform/system-config/listByType/${configType}`,
    method: 'get'
  });
}

/**
 * 查询系统配置详情
 */
export function fetchSystemConfigDetail(id: number) {
  return request<Api.DataPlatform.SystemConfig>({
    url: `/dataplatform/system-config/${id}`,
    method: 'get'
  });
}

/**
 * 根据配置键获取配置值
 */
export function fetchConfigValue(configKey: string) {
  return request<string>({
    url: `/dataplatform/system-config/value/${configKey}`,
    method: 'get'
  });
}

/**
 * 新增系统配置
 */
export function addSystemConfig(data: Api.DataPlatform.SystemConfigEdit) {
  return request({
    url: '/dataplatform/system-config',
    method: 'post',
    data
  });
}

/**
 * 修改系统配置
 */
export function updateSystemConfig(data: Api.DataPlatform.SystemConfigEdit) {
  return request({
    url: '/dataplatform/system-config',
    method: 'put',
    data
  });
}

/**
 * 批量更新配置
 */
export function batchUpdateSystemConfig(data: Api.DataPlatform.SystemConfigEdit[]) {
  return request({
    url: '/dataplatform/system-config/batchUpdate',
    method: 'put',
    data
  });
}

/**
 * 删除系统配置
 */
export function deleteSystemConfig(ids: number[]) {
  return request({
    url: `/dataplatform/system-config/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 验证配置
 */
export function validateConfig(configType: string) {
  return request<{ success: boolean; message: string; version?: string }>({
    url: `/dataplatform/system-config/validate/${configType}`,
    method: 'post'
  });
}

/**
 * 测试Doris连接
 */
export function testDorisConnection() {
  return request<{ success: boolean; message: string; version?: string }>({
    url: '/dataplatform/system-config/testDoris',
    method: 'post'
  });
}

/**
 * 测试DolphinScheduler连接
 */
export function testDolphinSchedulerConnection() {
  return request<{ success: boolean; message: string }>({
    url: '/dataplatform/system-config/testDolphinScheduler',
    method: 'post'
  });
}

/**
 * 导出配置
 */
export function exportConfig(configType: string) {
  return request<string>({
    url: `/dataplatform/system-config/export/${configType}`,
    method: 'get'
  });
}

/**
 * 导入配置
 */
export function importConfig(configJson: string) {
  return request({
    url: '/dataplatform/system-config/import',
    method: 'post',
    data: configJson,
    headers: {
      'Content-Type': 'text/plain'
    }
  });
}

/**
 * 刷新配置缓存
 */
export function refreshConfigCache() {
  return request({
    url: '/dataplatform/system-config/refreshCache',
    method: 'post'
  });
}
